package herguan.expertadvise.web.struts.action;


import herguan.expertadvise.dao.SpringHibernateDAO;
import herguan.expertadvise.dao.hibernate.Login;
import herguan.expertadvise.services.ExpertAdviseServiceFinder;
import herguan.expertadvise.web.struts.form.RatingsForm;
import herguan.expertadvise.web.struts.form.UserLoginForm;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

public class RatingsAction  extends Action{
	
	 public ActionForward execute(
			    ActionMapping mapping,
			    ActionForm form,
			    HttpServletRequest request,
			    HttpServletResponse response) throws Exception{
				  
		       
	       
		        //Retrieve the DAO Reference
		  	  SpringHibernateDAO springHibernateDAO = (SpringHibernateDAO) ExpertAdviseServiceFinder.getContext(request)
		  		.getBean("SpringHibernateDao");
		  	  
		  	  
//		  	Create object of ActionMesssages
		        ActionMessages errors = new ActionMessages();
		  	  
		        RatingsForm objForm = (RatingsForm) form;
		        HttpSession  session = request.getSession();
		           String struserid=session.getAttribute("ID").toString();
		           
		           //TODO -Get From the DB instead hardcoding
		           objForm.setQuestion("Why dog is animal?");
		           objForm.setAnswer("Dnt know");
		           objForm.setProvidername("Nidhi");
		           objForm.setQuestionid("1");
		           
		      	  
		           if (objForm.getAction().equals("addrating")) 
			        { 
		        	   if ((session.getAttribute("UserTypeId") != null && session.getAttribute("UserTypeId").toString().equals("0")))
			    	    
			 	       {
		        	   String strrating =objForm.getRating();		        	   
		        	   springHibernateDAO.addratings(struserid,strrating);
		        	   objForm.setRatingStatus("Your rating Has Been Saved");
		        	   
			            } 
			        else
			        {
			        	String stranswer =objForm.getSubmitedanswer();
			        	String questionid = objForm.getQuestionid();
			        	springHibernateDAO.addanswer(struserid,questionid,stranswer);
			        	
			        	
			        }
			        }
		           
		             return mapping.findForward("failure");
		         }
	       }
	
	
	
